﻿using System;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using Microsoft.Commerce.Proxy.PaymentInstrumentService;

namespace TestWebApplication.CommercePaymentInstrument.APIs
{
    public class CommercePaymentInstrumentGetKeyApi : CommercePaymentInstrumentApi
    {
        private GetKeyInput input;
        private GetKeyOutput output;
        private Guid keyGuid;

        public CommercePaymentInstrumentGetKeyApi(BasicHttpBinding binding, EndpointAddress address, X509Certificate2 clientCert, Guid keyGuid)
            : base(binding, address, clientCert)
        {
            this.keyGuid = keyGuid;
        }

        public KeySetKey Key { get; private set; }

        protected override AbstractResponse ExecuteInternal(PaymentInstrumentServiceChannel channel)
        {
            this.output = channel.GetKey(input);
            return this.output;
        }

        public override void PrepareRequestData()
        {
            this.input = new GetKeyInput()
            {
                KeyGuid = this.keyGuid,
            };
        }

        public override void PrepareResponseData()
        {
            if (this.output.KeySet.Length > 0)
            {
                this.Key = this.output.KeySet[0];
            }
        }
    }
}
